/**
 * 
 */
package vue.popup;

import java.awt.Color;
import java.awt.GridLayout;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import model.preprocessor.HypreParameter;

/**
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGHypreDialogParameter extends IGDialogParameter {
	private static final long	serialVersionUID		= 1L;

	public static final String	VERBOSITY_DESCRIPTION	= "Verbosity level: ";
	public static final String	NOT_USED				= "NOT USED";


	private HypreParameter		param;

	// private JButton timeoutButton;
	private JPanel				timeoutPanel;
	private JLabel				timeoutDescritpion;


	private JPanel				verbosityPanel;
	private JLabel				verbosityDescritpion;
	private JComboBox			verbosityBox;

	public IGHypreDialogParameter(HypreParameter param) {
		// TODO Auto-generated constructor stub
		this.param = param;
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		JPanel panel = new JPanel(new GridLayout(0, 1));

		// timeout
		timeoutPanel = new JPanel(new GridLayout(1, 2));

		timeoutDescritpion = new JLabel(TIMEOUT_DESCRIPTION);
		timeoutPanel.add(timeoutDescritpion);
		timeoutPanel.add(timeoutField);

		panel.add(timeoutPanel);


		// verbosity
		verbosityPanel = new JPanel(new GridLayout(1, 2));
		verbosityDescritpion = new JLabel(VERBOSITY_DESCRIPTION);
		verbosityBox = new JComboBox();
		verbosityBox.addItem(NOT_USED);
		for (int i = HypreParameter.MIN_VERBOSITY ; i <= HypreParameter.MAX_VERBOSITY ; i++)
			verbosityBox.addItem(i);

		verbosityPanel.add(verbosityDescritpion);
		verbosityPanel.add(verbosityBox);

		panel.add(verbosityPanel);

		add(panel);

		add(outPanel);


		refresh();
	}

	@Override
	public void refresh() {
		timeoutField.setText("" + param.getTimeOut());
		if (param.getOut() == null)
			outField.setText(lastDirectory.getPath() + "/");
		else
			outField.setText(param.getOut());

		if (param.getVerbosity() == HypreParameter.NOT_USED)
			verbosityBox.setSelectedItem(NOT_USED);
		else
			verbosityBox.setSelectedItem(param.getVerbosity());
	}

	@Override
	public boolean checkAndSetParameter() {
		// TODO A tester
		boolean good = true;

		// timeout
		String timeout = timeoutField.getText();
		if (timeout.isEmpty())
			param.setTimeOut(HypreParameter.NO_DELAY);
		else
			try
			{
				param.setTimeOut(Integer.parseInt(timeout));
			} catch (Exception e)
			{
				timeoutPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
				good = false;
			}


		// out
		if (new File(outField.getText()).exists())
		{
			outPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
			good = false;
		}
		else
			param.setOut(outField.getText());


		// verbosity
		if (((String) verbosityBox.getSelectedItem()).equals(NOT_USED))
			param.setVerbosity(HypreParameter.NOT_USED);
		else
			param.setVerbosity((Integer) verbosityBox.getSelectedItem());


		return good;
	}


}
